<?php
// thinhnc 2013-04-16
/*
Hien thi lich ranh cua giang vien dua vao tbl_lichranhgv va tbl_lichphanconggv
*/
ob_start();
include 'class/class.giangvien.php';	
$giangvien=new giangvien;
$pt=new phieuthu;
include 'connect/connect.php';

$schStart = '1-4-2013';
$schEnd = '30-4-2013';

if(isset($_POST['f_schSubmit']))
{
	if($_POST['f_schStart'] != '' && $_POST['f_schEnd'] != '')
	{
		$schStart = str_replace("/","-",$_POST['f_schStart']);
		$schEnd = str_replace("/","-",$_POST['f_schEnd']);
	}
}

$dateRange = $pt->tinhSoNgay($schStart,$schEnd);
$schMsg = "Hiển thị lịch rảnh của giảng viên từ <b>".$schStart."</b> đến <b>".$schEnd."</b>.";
//$schMsg .= " (".$dateRange." ngày)";

?>
<!DOCTYPE html>
<html>
<head>
<link href="Calendar/CalendarControl.css" rel="stylesheet" type="text/css">
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/script_thinhnc.js" type="text/javascript"></script>
<script src="Calendar/CalendarControl.js"language="javascript"></script>
</head>
<body>
<table width="100%" cellpadding="5px" cellspacing="0" border="1">
<tr>
<td width="20%" valign="top">Tên giảng viên</td>
<td width="80%" valign="top">
Lịch phân công giảng viên<br />
<form name="fSchedule" method="post">
Từ <input type="text" name="f_schStart" onfocus="showCalendarControl(this);" /> đến <input type="text" name="f_schEnd" onfocus="showCalendarControl(this);" />
<input type="submit" name="f_schSubmit" value="Xem lịch" />
</form>
<?php echo $schMsg; ?>
</td>
</tr>
<?php 
$query_giangvien = "SELECT id,f_hovaten as hovaten FROM tbl_giangvien ORDER BY f_tinnhiem DESC";

$sql_giangvien = mysql_query($query_giangvien);

while ($r_gv = mysql_fetch_array($sql_giangvien))
{
 ?>
<tr>
<td width="20%" valign="top">
<?php echo $r_gv['id'];
echo $r_gv['hovaten']; ?>
<div id="thongkegv_<?php echo $r_gv['id']; ?>"></div>
</td>
<td width="80%" valign="top">
<?php
// So Ngay ma gv do day trong khoang thoi gian schStart va schEnd
$soNgayDay = 0;
// get Lich Rach cua giang vien
$lichranh_gv = $giangvien->getLichRanhGV($r_gv['id']);

$query_lichphancong = "SELECT
			tbl_lichranhgv.gvid,
			tbl_lichranhgv.carid,
			tbl_lichphanconggv.carid AS cabid,
			tbl_lichphanconggv.taken_on,
			tbl_lichphanconggv.available_on
		FROM tbl_lichranhgv
		LEFT JOIN tbl_lichphanconggv ON tbl_lichranhgv.gvid = tbl_lichphanconggv.gvid
		AND tbl_lichranhgv.carid = tbl_lichphanconggv.carid
		WHERE tbl_lichranhgv.gvid = '".$r_gv['id']."'
		";

$sql_lichphancong = mysql_query($query_lichphancong) or die(mysql_error());

$lichphancong_gv = array();
$i = 0;
while ($r_lichgv = mysql_fetch_array($sql_lichphancong))
{
	$lichphancong_gv[$i]['gvid'] = $r_lichgv['gvid'];
	$lichphancong_gv[$i]['carid'] = $r_lichgv['carid'];
	// if($r_lichgv['taken_on'] != '')
	// {		
		// $lichphancong_gv[$i]['taken'] = 1;
	// }
	// else
		// $lichphancong_gv[$i]['taken'] = 0;
	$lichphancong_gv[$i]['taken_on'] = $r_lichgv['taken_on'];
	$lichphancong_gv[$i]['available_on'] = $r_lichgv['available_on'];
	$i++;
}	
// echo "<pre>";
// print_r($lichphancong_gv);
// echo "</pre>";die;
	echo "<table border='1' cellpadding='0' cellspacing='0' style='background: #fff'>";
	echo "<tr>";
	// month heading	
	for($j=1; $j<=$dateRange; $j++)
	{
		echo "<td width='20' height='20' align='center'><b>";
		$stDate = explode("-", $schStart);
		$datenum = gmdate("d", gmmktime(0, 0, 0, (int)$stDate[1], (int)$stDate[0], (int)$stDate[2]) + ($j-1)*86400);
		echo $datenum;
		echo "</b></td>";
	}
	echo "</tr>";
	// days of month
	for($i=1; $i<=3; $i++)
	{
		echo "<tr>";
		// 18:16 16/04/2013: can chinh lai mien gia tri de dung hop chon ngày from-to
		for($j=1; $j<=$dateRange; $j++)
		{
			echo "<td align='center' height='20'>";
			
			$stDate = explode("-", $schStart);
			$currentDate = gmdate("d-m-Y", gmmktime(0, 0, 0, (int)$stDate[1], (int)$stDate[0], (int)$stDate[2]) + ($j-1)*86400);
			
			$date = array($currentDate, "#FFF");
			$carid = $giangvien->getCaRID($date[0],$i);
			$gvid = $r_gv['id'];
			if (count($lichphancong_gv) > 0)
			{
				foreach($lichphancong_gv as $li)
				{
// echo "<pre>";
// print_r($li);
// echo "</pre>";die;
					if ($carid == $li['carid'] && $gvid == $li['gvid'])
					{
						if ($li['taken_on'] == '' && $li['available_on'] == '')
							$date[1] = "#A8FF9E";
						elseif ((soSanhNgay($date[0],$li['taken_on']) >= 0 && soSanhNgay($li['available_on'], $date[0]) >= 0))
						{
							$date[1] = "#FFB3D1";
							$soNgayDay++;
						}
						elseif ((soSanhNgay($date[0],$li['taken_on']) < 0 || soSanhNgay($li['available_on'], $date[0]) < 0))
						{
							if ($date[1] != "#FFB3D1")
								$date[1] = "#A8FF9E";
						}
					}					
				}
				echo "<div style=\"background:".$date[1]."; width: 20px; height: 20px\"></div>";
			}			
			echo "</td>";
		}
		echo "</tr>";
	}
	echo "</table>";
	// Tong hop so ngay day cua gv	
	
?>
<script>document.getElementById('thongkegv_<?php echo $r_gv['id']; ?>').innerHTML = "Số buổi lên lớp: <?php echo $soNgayDay; ?>";</script>
</td>
</tr>
<?php 
}
?>
</table>
</body>
</html>